package com.liyuan.help_e.service.impl;

import com.liyuan.help_e.component.EnumUtils;
import com.liyuan.help_e.dao.UserDao;
import com.liyuan.help_e.model.User;
import com.liyuan.help_e.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    UserDao userDao;

    @Override
    public User selectUserByUsername(String username) {
        return userDao.selectUserByUsername(username);
    }

    @Override
    public User selectUserByUserId(Integer id) {
        return userDao.selectUserByUserId(id);
    }

    @Override
    public void signin(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setAvatar_url("/images/default_avatar_url.png");
        user.setPassword(bCryptPasswordEncoder.encode(password));
        user.setRole(EnumUtils.ROLE_USER.getType());
        userDao.insertUser(user);
    }

    @Override
    public void updateUserAvatarUrl(String username, String avatarUrl) {
        userDao.updateUserAvatarUrl(username,avatarUrl);
    }
}
